import axios from "axios";
import { ElNotification } from 'element-plus'
import {setToken,getToken} from '@/composables/auth'
// import { useCookies } from '@vueuse/integrations/useCookies'

const service = axios.create({
    baseURL: "/api/v1"
})

// 添加请求拦截器
service.interceptors.request.use(function (config) {
    const token = getToken()
    if (token)
        config.headers["token"] = token
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
service.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    if (response.data.code == 200) {
        return response.data.data
    } else {
        ElNotification({
            title: 'Error',
            message: response.data.msg || '请求失败',
            type: 'error',
        })
        return Promise.reject(response);
    }
}, function (error) {
    // 对响应错误做点什么
    ElNotification({
        title: 'Error',
        message: '请求失败',
        type: 'error',
    })
    console.log("失败")
    return Promise.reject(error);
});

export default service